<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>Server Power Management</title>
    <link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='style.css') }}">
    
    <!-- JavaScript for confirmation dialogs -->
    <script>
      function confirmAction(event, url, message) {
        event.preventDefault(); // 阻止默认的点击行为
        if (confirm(message)) {
          window.location.href = url; // 如果用户确认，则跳转到指定 URL
        }
      }
    </script>
  </head>
  <body>
    <h1>Server Power Management</h1>

    <button id="theme-toggle">Toggle Light/Dark Mode</button>

    <button onclick="window.location.href='{{ url_for('add_server') }}'">Add A New Server</button>
    <table border="1">
      <thead>
        <tr>
          <th>Hostname</th>
          <th>BMC IP</th>
          <th>Username</th>
          <th>Password</th>
          <th>Use LAN Plus</th>
          <th>Power Status</th>
          <th>Action</th>
          <th>Edit</th>
          <th>Delete</th>
        </tr>
      </thead>
      <tbody>
        {% for server in servers %}
        <tr>
          <td>{{ server.hostname }}</td>
          <td>{{ server.bmc_ip }}</td>
          <td>{{ server.username }}</td>
          <td>
            <input type="password" id="password-{{ server.id }}" value="{{ server.password }}" disabled>
            <i class="eye-icon" id="toggle-password-{{ server.id }}" onclick="togglePasswordVisibility({{ server.id }})"></i>
          </td>
          <td>{{ server.use_lanplus }}</td>
          <td>{{ server.powerstat }}</td>
          <td>
            {% if server.powerstat == 'power on' %}
              <button onclick="confirmAction(event, '{{ url_for('power_action', id=server.id, action='soft') }}', 'Are you sure you want to perform a Soft Power Off?')">Soft Power Off</button>
	    &nbsp;|&nbsp;
              <button onclick="confirmAction(event, '{{ url_for('power_action', id=server.id, action='off') }}', 'Are you sure you want to perform a Hard Power Off?')">Hard Power Off</button>
            {% elif server.powerstat == 'power off' %}
              <button onclick="confirmAction(event, '{{ url_for('power_action', id=server.id, action='on') }}', 'Are you sure you want to power on the server?')">Power On</button>
            {% else %}
            Unknown
            {% endif %}
          </td>
          <td><button onclick="window.location.href='{{ url_for('edit_server', id=server.id) }}'">Edit</button></td>
          <td>
            <form method="POST" action="{{ url_for('delete_server', id=server.id) }}" style="display:inline;">
              <button type="submit" onclick="return confirm('Are you sure you want to delete this server?')">Delete</button>
            </form>
          </td>
        </tr>
        {% endfor %}
      </tbody>
    </table>
    <script src="{{ url_for('static', filename='script.js') }}"></script>
  </body>
</html>